
package ui.gui.table.renderer;

import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.TableCellRenderer;

/**
 * Renderer class for decimals
 * @author Mads Nielsen
 */
public class DecimalCellRenderer extends DefaultTableCellRenderer implements TableCellRenderer {

    private int precision;

    /**
     * Constructor for class. Uses default value for precision
     */
    public DecimalCellRenderer() {
        this.precision = 2;
    }

    /**
     * Constructor with specified precision of decimals
     * @param precision Precision of rendered decimals
     */
    public DecimalCellRenderer(int precision) {
        this.precision = precision;
    }
    
    private String getFormat() {
        return "%." + precision + "f";
    }

    @Override
    protected void setValue(Object value) {

        value = String.format(getFormat(), value);

        super.setValue(value);
        setHorizontalAlignment(RIGHT);
    }

}
